package youleche.com.sql.business;

import java.sql.Connection;
import java.util.List;
import java.util.Map;


import youleche.com.sql.factory.ConnectionFactory;
import youleche.com.sql.factory.DaoFactory;
import youleche.com.sql.factory.IDFactory;
import youleche.com.sql.idao.ITableInfo;
import youleche.com.sql.model.MTableInfo;
import youleche.com.util.DateUtil;

/**
 * 表信息业务层
 * @author Administrator
 *
 */
public class BTableInfo {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 表处理器
	 */
	private ITableInfo tableHandler = null;

	/**
	 * 构造器
	 */
	public BTableInfo() {
		this.tableHandler = DaoFactory.getDTableInfo();
	}
	
	/**
	 * 添加表信息
	 * @param tableInfo 表信息
	 * @return 返回添加影响的行数
	 */
	public int addTableInfo(MTableInfo tableInfo) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableWriteConnection();
			tableInfo.setTableId(IDFactory.getId("autotable", "tableInfo"));
			tableInfo.setCreateTime(DateUtil.getDate());
			tableInfo.setModifyTime(DateUtil.getDate());
			return this.tableHandler.addTableInfo(this.con, tableInfo);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

	public static void main(String[] args) {
		int i =1;
		while(i < 10000){
			MTableInfo tableInfo = new MTableInfo();
			tableInfo.setCreateUser(i + "-shenyuc");
			tableInfo.setTableName(i + "-userInfo");
			tableInfo.setTableDesc( i +"tableDesc");
			BTableInfo b = new BTableInfo();
			try {
				b.addTableInfo(tableInfo);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			System.out.println(i);
			i++;
		}
	}
	/**
	 * 是否存在该表
	 * @param tableName 表名
	 * @return 返回是否存在
	 * @throws Exception
	 */
	public boolean isExistTableName(String tableName) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.isExistTableName(this.con, tableName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 删除表信息通过ID
	 * @param tableId 表ID
	 * @return 返回是否删除成功
	 * @throws Exception 抛出异常
	 */
	public boolean deleteTableInfoById(String tableId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getAutoTableWriteConnection();
			return this.tableHandler.deleteTableInfoById(this.con, tableId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取表数据总数
	 * @return 返回总条数
	 * @throws Exception 抛出异常
	 */
	public int getTableInfoCount() throws Exception {
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.getTableInfoCount(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 分页
	 * @param startIndex 开始索引
	 * @param endIndex 结束索引
	 * @return 返回分页数据
	 * @throws Exception 抛出异常
	 */
	public List<MTableInfo> getTableInfoListForPage(
			String startIndex, String endIndex) throws Exception {
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.getTableInfoListForPage(this.con, startIndex, endIndex);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据tableID获取表数据信息
	 * @param tableId 表ID
	 * @return 表信息
	 * @throws Exception
	 */
	public MTableInfo getTableInfoByTableId(String tableId) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.getTableInfoByTableId(this.con, tableId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过tableId更新说明
	 * @param tableId 表ID
	 * @param tableDesc 表说明
	 * @return  返回影响的行数
	 * @throws Exception
	 */
	public int updateTableDescByTableId(String tableId,
			String tableDesc) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.updateTableDescByTableId(this.con, tableId, tableDesc);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 分页查询
	 * @param condition 查询条件
	 * @param 
	 * @return 返回查询结果
	 * @throws Exception 抛出异常
	 */
	public List<MTableInfo> getTableInfoListForQuery(
			Map<String, String> condition, String startIndex, String pageSize) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.getTableInfoListForQuery(this.con, condition, startIndex, pageSize);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取表数据总数(查询)
	 * @param condition 查询条件
	 * @return 返回总条数
	 * @throws Exception 抛出异常
	 */
	public int getTableInfoCountForQuery(Map<String, String> condition) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.getTableInfoCountForQuery(this.con, condition);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据表名获取表id
	 * @param tableName 表名
	 * @return 返回表id
	 * @throws Exception 异常
	 */
	public String getTableIdByTableName(String tableName) throws Exception{
		try{
			this.con = ConnectionFactory.getAutoTableReadConnection();
			return this.tableHandler.getTableIdByTableName(this.con, tableName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
